import { reqgoodslist, reqgoodscount } from '../../http/api'
const state = {
    goods_Arr: [],
    //总条目
    total: 0,
    // 一页的数量
    size: 2,
    //页码
    page: 1
}

const getters = {
    goods_Arr(state) {
        return state.goods_Arr
    },
    total(state) {
        return state.total
    },
    size(state) {
        return state.size
    },
    page(state) {
        return state.page
    }
}

const mutations = {
    set_goodsArr(state, data) {
        state.goods_Arr = data
    },
    set_total(state, data) {
        state.total = data
    },
    set_size(state, data) {
        state.size = data
    },
    set_page(state, data) {
        state.page = data
    }
}

const actions = {
    set_goodsArr(context, data) {
        reqgoodslist(data).then(res => {
            if (res.data.code === 200) {
                context.commit('set_goodsArr', res.data.list)
            }
        }).catch((err) => {
            console.log('请求失败-----err:' + err);
        })
    },
    set_total(context) {
        reqgoodscount().then(res => {
            if (res.data.code === 200) {
                context.commit('set_total', res.data.list[0].total)
            }
        }).catch((err) => {
            console.log('请求失败-----err:' + err);
        })
    },
    set_page(context, data) {
        context.commit('set_page', data)
    },
    set_size(context, data) {
        context.commit('set_size', data)
    }
}

export default {
    state,
    getters,
    mutations,
    actions,
    //命名空间
    namespaced: true
}